<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;
use App\Models\Depot\Back as BackModel;
class Order extends Model
{

    protected $table = 'order';

    protected $dateFormat = 'U';

    const CREATED_AT = 'createtime';

    const UPDATED_AT = null;

    protected $guarded = [];
    protected $casts = [
        'createtime' => 'datetime:Y-m-d H:i:s',
    ];

    protected $appends = [
        'status_text', // 订单状态
        'depot_text', //退货状态
    ];

    public function getStatusTextAttribute()
    {
        $status = $this->attributes['status'];

        switch ($status) {
            case '0':
                return '待支付';
            case '1':
                return '待发货';
            case '2':
                return '待收货';
            case '3':
                return '待评价';
            case '4':
                return '已完结';
            case '-1':
                return '仅退款';
            case '-2':
                return '退款退货';
            case '-3':
                return '待退货';
            case '-4':
                return '退货成功';
            case '-5':
                return '退货失败';
        }
    }
    public function getDepotTextAttribute()
    {
        $code = !empty($this->attributes['code']) ? $this->attributes['code'] : '';

        $status = true;

        // 查询退货单信息
        $back = BackModel::where('ordercode', $code)->first();

        if ($back) {
            $status = false;
        }

        return $status;
    }
}